package com.coocaa.ops.admin.v1.plugin.internal;

import com.coocaa.ops.admin.v1.plugin.biz.IPluginInfoBiz;
import com.coocaa.ops.common.tool.json.JsonUtil;
import com.coocaa.ops.plus.core.base.annotation.MyAuth;
import com.coocaa.ops.plus.core.base.enums.MyRoleEnums;
import com.coocaa.ops.plus.dashboard.v1.plugin.model.InternalPluginModel;
import com.coocaa.ops.admin.v1.plugin.provider.IPluginUpdateProvider;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.biz.plugin.entity.PluginInfoEntity;
import com.coocaa.ops.common.core.security.annotation.MyGuard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author tangminghong
 * @date 2020-09-16
 */
@RestController
@RequestMapping("/myExtend/v1/plugin/internal")
public class InternalPluginConfController {

    @Autowired
    private IPluginUpdateProvider pluginUpdateProvider;

    @Autowired
    private IPluginInfoBiz pluginInfoBiz;

    @MyAuth(validAuth = true, hasRoles = {MyRoleEnums.ROLE_ADMIN})
    @RequestMapping("/getDetailInfo")
    public Object getDetailInfo(@RequestParam("id") Integer id) {
        //PluginInfoEntity plugin = (PluginInfoEntity)JsonUtil.json2Object(queryModel.getJsonStr(), PluginInfoEntity.class);
        return R.ok().data(pluginInfoBiz.queryById(id));
    }

    @MyAuth(validAuth = true, hasRoles = {MyRoleEnums.ROLE_ADMIN})
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    public Object addOrUpdate(@RequestParam("jsonStr") String jsonStr) {
        PluginInfoEntity plugin = (PluginInfoEntity)JsonUtil.json2Object(jsonStr, PluginInfoEntity.class);
        if (plugin.getId() != null && plugin.getId() != 0) {
            pluginInfoBiz.updateById(plugin);
        } else {
            pluginUpdateProvider.addOrUpdatePluginInfo(plugin);
        }
        return R.ok();
    }
}
